package com.kmvc.message.activity;

import com.kmvc.message.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MessageSetting extends Activity implements OnClickListener {

	private boolean DownloadState;
	private SharedPreferences sp;
	private CheckBox isDownloadBox;
	private Editor editor;
	private TextView show_MyFavorites,show_mywallet,show_autoClear;
	private String[] data={"最小","较小","正常","较大","最大"};

	public void onCreate(Bundle parambBundle) {
		super.onCreate(parambBundle);
		View view = a(getLayoutInflater());
		setContentView(view);
		TextView nick = (TextView) findViewById(R.id.nick);
		TextView update_pwd = (TextView) findViewById(R.id.update_pwd);
		TextView autoClear = (TextView) findViewById(R.id.autoClear);
		show_autoClear=(TextView)findViewById(R.id.show_autoClear);
		Button logoutBtn = (Button) findViewById(R.id.logoutBtn);
		TextView my_wallet = (TextView) findViewById(R.id.my_wallet);
		show_mywallet=(TextView)findViewById(R.id.show_my_wallet);
		TextView my_favorites = (TextView) findViewById(R.id.my_favorites);
		show_MyFavorites = (TextView)findViewById(R.id.show_my_favorites);
		TextView auto_down_img = (TextView) findViewById(R.id.auto_download_img);
		isDownloadBox = (CheckBox) findViewById(R.id.isDownload);
		TextView about = (TextView) findViewById(R.id.about);

		sp = getSharedPreferences("com.kmvc.message_preferences", 0);
		if (sp != null) {
			DownloadState = sp.getBoolean("AutoDownloadImg", false);
			editor=sp.edit();
			IsDownLoadImage();
		}
		SharedPreferences shp = getSharedPreferences("phonenumber", 0);
		if (shp != null) {
			nick.setText(shp.getString("phone", ""));
		}
		
		show_autoClear.setTextColor(Color.GRAY);
		show_mywallet.setTextColor(Color.GRAY);
		show_MyFavorites.setTextColor(Color.GRAY);
		
		findViewById(R.id.ivTitleBtnLeft).setVisibility(4);
		((TextView) findViewById(R.id.ivTitleName)).setText("设置");

		update_pwd.setOnClickListener(this);
		autoClear.setOnClickListener(this);
		my_wallet.setOnClickListener(this);
		my_favorites.setOnClickListener(this);
		auto_down_img.setOnClickListener(this);
		about.setOnClickListener(this);
		logoutBtn.setOnClickListener(this);
		if (VERSION.SDK_INT >= 8) {
			getWindow().addFlags(67108864);

		}
	}

	public View a(LayoutInflater paramLayoutInflater) {
		return paramLayoutInflater.inflate(R.layout.qqsetting, null);
	}

	@Override
	public void onClick(View paramView) {
		int id = paramView.getId();
		switch (id) {
		case R.id.update_pwd:
			Intent intent1 = new Intent();
			intent1.putExtra("flag", false);
			intent1.setClass(MessageSetting.this,
					GesturePWDCreateActivity.class);
			this.startActivity(intent1);
			break;
		case R.id.autoClear:
			Intent intent2 = new Intent(MessageSetting.this,
					AutoClearActivity.class);
			startActivity(intent2);
			break;
		case R.id.my_wallet:
			Intent intent3 = new Intent(MessageSetting.this,
					SetTextSizeActivity.class);
			startActivity(intent3);
			break;
		case R.id.my_favorites:
			Intent intent4 = new Intent(MessageSetting.this,
					SetUpdateRateActivity.class);
			startActivity(intent4);
			break;
		case R.id.auto_download_img:
			IsDownLoadImage();
			break;
		case R.id.about:
			Intent intent5 = new Intent(this, AboutActivity.class);
			startActivity(intent5);
			break;
		case R.id.logoutBtn:
			this.finish();
			System.exit(0);
			break;
		}
	}

	private void IsDownLoadImage() {
		if (DownloadState == false) {
			isDownloadBox.setChecked(DownloadState);
			editor.putBoolean("AutoDownloadImg", DownloadState);
			editor.commit();
			DownloadState = true;
		} else if (DownloadState == true) {
			isDownloadBox.setChecked(DownloadState);
			editor.putBoolean("AutoDownloadImg", DownloadState);
			editor.commit();
			DownloadState = false;
		}
	}
	
	@Override
	public void onResume(){
		super.onResume();
		if(sp.getBoolean("isclear", false) & sp.getString("timelist", "").length()>0){
			show_autoClear.setText(Integer.parseInt(sp.getString("timelist", ""))+1+"天前");
		}else{
			show_autoClear.setText("未开启");
		}
		if(sp.getString("sizelist", "").length()>0){
			show_mywallet.setText(data[Integer.parseInt(sp.getString("sizelist", ""))]);
		}
		if(sp.getString("pllist", "").length()>0){
			show_MyFavorites.setText(sp.getString("pllist", "")+"分钟");
		}
	}
}
